package com.yuanyu.server.impl;

import com.yuanyu.common.util.AssertUtil;
import com.yuanyu.common.util.CopyUtil;
import com.yuanyu.mapper.mysqlMapper.MainTable_Mapper;
import com.yuanyu.mapper.mysqlMapper.Repair_and_Disassembly_Report.Repair_and_Disassembly_Report_1_Mapper;
import com.yuanyu.mapper.mysqlMapper.Repair_and_Disassembly_Report.Repair_and_Disassembly_Report_2_Mapper;
import com.yuanyu.mapper.mysqlMapper.Repair_and_Disassembly_Report.Repair_and_Disassembly_Report_Mapper;
import com.yuanyu.model.dto.Repair_and_Disassembly_ReportDTO;
import com.yuanyu.model.entity.Repair_and_Disassembly_Report.Repair_and_Disassembly_Report;
import com.yuanyu.model.entity.Repair_and_Disassembly_Report.Repair_and_Disassembly_Report_1;
import com.yuanyu.model.entity.Repair_and_Disassembly_Report.Repair_and_Disassembly_Report_2;
import com.yuanyu.model.vo.Checked_The_cause_of_the_failure;
import com.yuanyu.model.vo.Repair_and_Disassembly_ReportVO;
import com.yuanyu.server.ITestPart2Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional
public class TestPart2ServerImpl implements ITestPart2Server {
    @Autowired
    private Repair_and_Disassembly_Report_Mapper repairAndDisassemblyReportMapper;
    @Autowired
    private Repair_and_Disassembly_Report_1_Mapper repairAndDisassemblyReport1Mapper;
    @Autowired
    private Repair_and_Disassembly_Report_2_Mapper repairAndDisassemblyReport2Mapper;
    @Autowired
    private MainTable_Mapper mainTableMapper;

    @Override
    public String test() {
        return "测试成功";
    }

    @Override
    public String test2(Repair_and_Disassembly_ReportVO repairAndDisassemblyReportVO) {
        //先判断Case是否存在
        Repair_and_Disassembly_Report test = repairAndDisassemblyReportMapper.selTableTime(repairAndDisassemblyReportVO.getInput_Case());
        AssertUtil.isNull(test,"Case已存在,如需修改内容请前往后台修改");
        //时间表
        Repair_and_Disassembly_Report repairAndDisassemblyReport = new Repair_and_Disassembly_Report();
        //子表1
        Repair_and_Disassembly_Report_1 repairAndDisassemblyReport1 = new Repair_and_Disassembly_Report_1();
        //子表2
        Repair_and_Disassembly_Report_2 repairAndDisassemblyReport2 = new Repair_and_Disassembly_Report_2();
        //时间表 获取VO类对应的值
        CopyUtil.copyClass(repairAndDisassemblyReportVO,repairAndDisassemblyReport);
        //时间表 获取当前时间
        repairAndDisassemblyReport.setCreatedAt(LocalDateTime.now());
        repairAndDisassemblyReport.setUpdatedAt(LocalDateTime.now());
        //子表1 获取VO类对应的值
        CopyUtil.copyClass(repairAndDisassemblyReportVO,repairAndDisassemblyReport1);
        //子表2 获取VO类对应的值
        CopyUtil.copyClass(repairAndDisassemblyReportVO,repairAndDisassemblyReport2);
        //调用子表2的input方法使T1 -> T14获得Checked_The_cause_of_the_failure的值
        repairAndDisassemblyReport2.input();

        System.out.println(repairAndDisassemblyReport);
        System.out.println(repairAndDisassemblyReport1);
        System.out.println(repairAndDisassemblyReport2);

        //判断是否出现异常,出现异常返回"添加数据出现异常,请检查",并且回滚数据
        int i = repairAndDisassemblyReportMapper.addTableTime(repairAndDisassemblyReport);
        int i1 = repairAndDisassemblyReport1Mapper.addTable1(repairAndDisassemblyReport1);
        int i2 = repairAndDisassemblyReport2Mapper.addTable2(repairAndDisassemblyReport2);
        AssertUtil.isTrue(i == 1 && i1 == 1 && i2 == 1,"添加数据出现异常,请检查");
        mainTableMapper.updateByCaseFormStartTime(repairAndDisassemblyReportVO.getInput_Case());
        return "添加成功";
    }

    @Override
    public Repair_and_Disassembly_ReportDTO getTest2(String Case) {
        //获取每个子表的信息,并判断,如果不存在就返回"Case不存在"
        Repair_and_Disassembly_Report repairAndDisassemblyReport = repairAndDisassemblyReportMapper.selTableTime(Case);
        if (repairAndDisassemblyReport == null)return null;
        Repair_and_Disassembly_Report_1 repairAndDisassemblyReport1 = repairAndDisassemblyReport1Mapper.selTable1(Case);
        Repair_and_Disassembly_Report_2 repairAndDisassemblyReport2 = repairAndDisassemblyReport2Mapper.selTable2(Case);
        /*
        * 使Checked_The_cause_of_the_failure获得repairAndDisassemblyReport2的T1 -> T14_value的值,
        * 然后再把Checked_The_cause_of_the_failure传值到repairAndDisassemblyReport2
        *
        * 目的就是使repairAndDisassemblyReport2的Checked_The_cause_of_the_failure获取到T1 -> T14_value的值
        * */
        Checked_The_cause_of_the_failure checkedTheCauseOfTheFailure = new Checked_The_cause_of_the_failure();
        CopyUtil.copyClass(repairAndDisassemblyReport2,checkedTheCauseOfTheFailure);
        repairAndDisassemblyReport2.setChecked_The_cause_of_the_failure(checkedTheCauseOfTheFailure);
        //new一个DTO类
        Repair_and_Disassembly_ReportDTO repairAndDisassemblyReportDTO = new Repair_and_Disassembly_ReportDTO();
        //把子类的值拷贝到DTO类
        CopyUtil.copyClass(repairAndDisassemblyReport,repairAndDisassemblyReportDTO);
        CopyUtil.copyClass(repairAndDisassemblyReport1,repairAndDisassemblyReportDTO);
        CopyUtil.copyClass(repairAndDisassemblyReport2,repairAndDisassemblyReportDTO);
        return repairAndDisassemblyReportDTO;
    }

    @Override
    public String updateTable2(Repair_and_Disassembly_ReportVO repairAndDisassemblyReportVO) {
        //时间表
        Repair_and_Disassembly_Report repairAndDisassemblyReport = new Repair_and_Disassembly_Report();
        //子表1
        Repair_and_Disassembly_Report_1 repairAndDisassemblyReport1 = new Repair_and_Disassembly_Report_1();
        //子表2
        Repair_and_Disassembly_Report_2 repairAndDisassemblyReport2 = new Repair_and_Disassembly_Report_2();
        //时间表 获取VO类对应的值
        CopyUtil.copyClass(repairAndDisassemblyReportVO,repairAndDisassemblyReport);
        //时间表 获取当前时间
        repairAndDisassemblyReport.setUpdatedAt(LocalDateTime.now());
        //子表1 获取VO类对应的值
        CopyUtil.copyClass(repairAndDisassemblyReportVO,repairAndDisassemblyReport1);
        //子表2 获取VO类对应的值
        CopyUtil.copyClass(repairAndDisassemblyReportVO,repairAndDisassemblyReport2);
        //调用子表2的input方法使T1 -> T14获得Checked_The_cause_of_the_failure的值
        repairAndDisassemblyReport2.input();

        int i = repairAndDisassemblyReportMapper.updateTableTime(repairAndDisassemblyReport);
        int i1 = repairAndDisassemblyReport1Mapper.updateTable1(repairAndDisassemblyReport1);
        int i2 = repairAndDisassemblyReport2Mapper.updateTable2(repairAndDisassemblyReport2);
        AssertUtil.isTrue(i == 1 && i1 == 1 && i2 == 1,"表2修改数据出现异常,请检查");
        return "修改成功";
    }
}
